package com.yjzx.util.common.util.batch.mergingLimit;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @author guozhi
 * @date 2025/1/13
 * @description NotNullParam
 */
public interface NotNullParam {
    default void check() {
        check(getClass());
    }

    default void check(Class<?> clazz, Field... fields) {
        if (clazz != null) {
            Set<Field> set = fields != null && fields.length > 0 ? new HashSet<>(Arrays.asList(fields)) : Collections.emptySet();
            for (Field field : clazz.getDeclaredFields()) {
                try {
                    if (set.contains(field)) {
                        continue;
                    }
                    field.setAccessible(true);
                    if (field.get(this) == null) {
                        throw new NullPointerException(getClass().getName() + " " + field.getName() + " is null");
                    }
                    Class<?> superclass = clazz.getSuperclass();
                    if (superclass != null && !superclass.isArray()) {
                        check(superclass,fields);
                    }
                } catch (IllegalAccessException ignored) {
                }
            }
        }
    }
}
